import java.util.Iterator;
import java.util.Vector;

/**
 * Class holding the structure of an Intersection.
 * @author Jared Lindbl�m
 */
public class Intersection {
	private String name;
	private Vector<Road> roads;

	/**
	 * Constructor. Like RoadMap, the number of roads can be 
	 * specified to set an initial size of the vector.
	 * @param name		The name of the Intersection.
	 * @param numRoads	The number of roads that will be associated with the intersection.
	 */
	public Intersection (String name, int numRoads) {
		this.name = name;
		roads = new Vector<Road>(numRoads);
	}

	/**
	 * Constructor.
	 * @param name	The name of the Intersection.
	 */
	public Intersection (String name) {
		this.name = name;
		roads = new Vector<Road>();
	}

	/**
	 * Accessor Method (Name).
	 * @return	The name of the Intersection.
	 */
	public String getName() {
		return name;
	}

	/**
	 * Accessor Method (Roads).
	 * @param currentRoad	The current road the vehicle is on.
	 * @return The list of associated roads to this intersection, minus the current road.
	 */
	public Road[] getRoads(Road currentRoad) {
		@SuppressWarnings("unchecked")
		Vector<Road> roadRemoved = (Vector<Road>) roads.clone();

		roadRemoved.removeElement(currentRoad);
		Road[] r = new Road[roadRemoved.size()];

		roads.toArray(r);
		return r;
	}

	/**
	 * Associates a road with the intersection.
	 * @param road	The road to be associated to this intersection.
	 */
	public void addRoad(Road road) {
		roads.addElement(road);
	}

	/** 
	 * Prints the structure of this intersection (for debugging).
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		String print = "Name: " + name + "\n";

		Iterator<Road> it = roads.iterator();

		print = print.concat("\n\t\t\tAssociated Roads: (" + roads.size() + " in total)\n\n");
		while(it.hasNext()) {
			print = print.concat("\t\t\t\tName: " + (it.next()).getName() + "\n");
		}

		return print;
	}
}